/****************************************************************************
    Copyright 2009 Clark Scheff
    
    This file is part of Boulder Crush.

    Boulder Crush is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation version 3 of the License.

    Boulder Crush is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Boulder Crush.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

package com.scheffsblend.boulder.crash;

import android.app.Activity;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class BoulderCrash extends Activity {

	private GraphView mGraphView;
	private SensorManager mSensorManager;
	private AudioManager mAudioManager = null;
	private Menu mMenu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		mGraphView = new GraphView(this);
		mGraphView.setFocusable(true);
		mGraphView.setFocusableInTouchMode(true);
		setContentView(mGraphView);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mSensorManager.registerListener(mGraphView, 
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	protected void onStop() {
		mSensorManager.unregisterListener(mGraphView);
		mGraphView.stop();
		super.onStop();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
     
		// Parameters for menu.add are:
		// group -- Not used here.
		// id -- Used only when you want to handle and identify the click yourself.
		// title
		mMenu = menu;
		mMenu.add(0, 0, 0, "Toggle Sound");
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		case 0:
			boolean sounds = mGraphView.getSounds();
			mGraphView.setSounds(!sounds);
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if(event.getAction() == KeyEvent.ACTION_DOWN) {
			switch(event.getKeyCode()) {
			case KeyEvent.KEYCODE_VOLUME_UP:
				mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
				return true;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
				return true;
			}
		}
		return false;
	}
}
